<!DOCTYPE html>
<html>
<title>Registration Page :: DIF233 - Unit 1 - Excercise 1</title>
<body>
	<?php
		function is_user_exist($user_name){
			$file = fopen("shadow.txt","a+") or exit("Sorry not able to open the register file");
			while(!feof($file)){
				$line = fgets($file);
				$value = explode(":", $line);
				if(strcmp($value[1], $user_name) == 0){
					return 1;
				}
			}
			return 0;
		}
		function save($name,$user_id,$password,$mobile_number,$email_id){
			$file = fopen("shadow.txt","a+") or exit("Sorry not able to save the detail");
			fputs($file, $name.":");
			fputs($file, $user_id.":");
			fputs($file, $password.":");
			fputs($file, $mobile_number.":");
			fputs($file, $email_id);
			fputs($file,"\n");
			fclose($file);
		}
		if(!empty($_POST["submit"])){
			if(is_user_exist($_POST["user_id"])){
				echo "<h3>User Already exists please try with different name</h3>";
			}else{
				save($_POST["name"],$_POST["user_id"],$_POST["password"],$_POST["mobile_number"],$_POST["email_id"]);
				header("Location: http://localhost/dif233/ch1/excercise1/index.php?msg=success");
				exit();
			}
		}	
	?>
<form method="post" name="register" action="register.php">
	<table border="0" width="100%">
		<tr>
			<td>
				Name:
			</td>
			<td>
				<input type="text" name="name" value=<?php echo '"', $_POST["name"], '"' ?> required/>
			</td>
		</tr>
		<tr>
			<td>
				User Name:
			</td>
			<td>
				<input type="text" name="user_id" required/>
			</td>
		</tr>
		<tr>
			<td>
				Password:
			</td>
			<td>
				<input type="password" name="password" value=<?php echo '"', $_POST["password"], '"' ?> required/>
			</td>
		</tr>		
		<tr>
			<td>
				Mobile Number:
			</td>
			<td>
				<input type="text" name="mobile_number" value=<?php echo '"', $_POST["mobile_number"], '"' ?> required/>
			</td>
		</tr>		
		<tr>
			<td>
				Email Id:
			</td>
			<td>
				<input type="email" name="email_id" value=<?php echo '"', $_POST["email_id"], '"' ?> required/>
			</td>
		</tr>
		<tr>
			<td colspan="2"><input type="submit" name="submit" value="submit"/></td>
		</tr>		
	</table>
</form>
</body>
</html>
